using System;
using System.Collections.Generic;
using System.Html;
using System.Runtime.CompilerServices;
using Awaiter;
using Nsb.Classes;
using ThemeChain;
using jQueryApi;

namespace WhirliGig.Classes
{
    [IgnoreNamespace]
    public class GigShowSlides : GigShow
    {
        public void ChangeSlideEv(jQueryEvent e)
        {
            RestoreSlideAw(Await.AsyncAw, Number.ParseInt(jQuery.FromElement(e.Target).GetValue()));
            StopSlidesEv(e);
        }

        public void NextSlide()
        {
            PageSelectToEl.Children().FadeOut(SlideFade);
            new Await()
                .Sleep(SlideFade)
                .AddDx(
                    delegate
                    {
                        RestoreSlideAw(Await.AsyncAw, Slide + 1 >= Slides.Count ? 0 : Slide + 1);
                        jQuery.Select("select", SlideControls).Value(Slide.ToString());
                    })
                .Commit();
        }

        public void NextSlideEv(jQueryEvent e)
        {
            RestoreSlideAw(Await.AsyncAw, Slide + 1 >= Slides.Count ? 0 : Slide + 1);
            jQuery.Select("select", SlideControls).Value(Slide.ToString());
            StopSlidesEv(e);
            DomElement.CancelEvent(e);
        }

        public void PrevSlideEv(jQueryEvent e)
        {
            RestoreSlideAw(Await.AsyncAw, Slide - 1 < 0 ? Slides.Count - 1 : Slide - 1);
            jQuery.Select("select", SlideControls).Value(Slide.ToString());
            StopSlidesEv(e);
            DomElement.CancelEvent(e);
        }

        public void AddSlideEv(jQueryEvent e)
        {
            StopSlidesEv(e);
            new Await()
                .AddDx(
                    delegate
                    {
                        SlideData slide = new SlideData();
                        slide.slide = Slide = Slides.Count;
                        slide.gigs = new Dictionary<string, GigData>();

                        Slides.Add(slide);
                        new Await()
                            .AddAw(RestoreSlideAw, Slide)
                            .AddDl(
                                delegate(Await aw)
                                {
                                    RedrawPageGear();
                                    IGigShowEdit ths = (IGigShowEdit) this;
                                    ths.SaveAllAw(aw);
                                })
                            .Commit();
                    })
                .Commit();
        }

        public void RemSlideEv(jQueryEvent e)
        {
            StopSlidesEv(e);
            new Await()
                .AddAw(new Ask().OkCancelAw, ThemeWhirliGig.DeleteMsg)
                .AddAw(DomElement.RequireResultAw, true)
                .AddDx(
                    delegate
                    {
                        Slides.RemoveAt(Slide);
                        if (Slides.Count == 0)
                            Slides.Add(BlankSlide());
                        int cnt = 0;
                        foreach (SlideData sd in Slides)
                        {
                            sd.slide = cnt++;
                        }
                        new Await()
                            .AddAw(RestoreSlideAw, Math.Max(Slide - 1, 0))
                            .AddDl(
                                delegate(Await aw)
                                {
                                    RedrawPageGear();
                                    IGigShowEdit ths = (IGigShowEdit) this;
                                    ths.SaveAllAw(aw);
                                })
                            .Commit();
                    })
                .Commit();
        }

        public void PlaySlidesEv(jQueryEvent e)
        {
            Window.ClearInterval(_playTmr);
            _playTmr = Window.SetInterval(NextSlide, CurrentSlide.interval);
            RestoreSlideAw(Await.AsyncAw, Slide);
            RedrawPageGear();
        }

        public void StopSlidesEv(jQueryEvent e)
        {
            if (_playTmr > 0)
            {
                Window.ClearInterval(_playTmr);
                _playTmr = 0;
                RestoreSlideAw(Await.AsyncAw, Slide);
            }
            RedrawPageGear();
        }
    }
}